<?php

namespace App\Http\Controllers\Backend;

use Illuminate\Support\Facades\Crypt;
use App\Models\Admin;

class AccountController extends BaseController
{
    // 个人账户

    // 初始化
    public function initialize()
    {
        parent::initialize();
    }

    // 修改
    public function edit()
    {
        $this->throwIf(empty($this->myUser), '请登录后在操作！！', LOGIN_ERROR);

        $data = [
            //'username' => $this->getData('username', null, 'trim'),
            //'role'     => $this->getData('role', null, 'trim'),
            'nickname' => $this->getData('nickname', null, 'trim'),
            'password' => $this->getData('password', null, 'trim'),
            'mobile'   => $this->getData('mobile', null, 'trim'),
            //'status'   => $this->getData('status', 1, 'intval),
        ];

        // 检查唯一性
        $model = new Admin();
        $data  = $this->filterNull($data);
        $data  = $model->checkData($data);
        $this->throwIf($data === false, $model->getError());

        // 验证
        $result = $model->checkRules($data, 'info');
        $this->throwIf($result === false, $model->getError());

        $where  = ['id' => $this->myUser['id']];
        $result = $model->oneEdit($where, $data);
        $this->throwIf($result === false, $model->getError());

        return $this->success();
    }

    // 修改密码
    public function password()
    {
        $this->throwIf(empty($this->myUser), '请登录后在操作！！', LOGIN_ERROR);

        $data = ['password' => $this->getData('password', null, 'trim'),];
        // 检查唯一性
        $model = new Admin();
        $data  = $this->filterNull($data);
        $data  = $model->checkData($data);
        $this->throwIf($data === false, $model->getError());

        $where  = ['id' => $this->myUser['id']];
        $result = $model->oneEdit($where, $data);
        $this->throwIf($result === false, $model->getError());

        return $this->success();
    }

    // 登录
    public function login()
    {
        // 验证
        $username = $this->getData('username', null, 'trim');
        $password = $this->getData('password', null, 'trim');

        // TODO 验证验证码


        // 登录操作
        $admin = new Admin();
        $user  = $admin->login($username, $password);  // 验证密码
        $this->throwIf($user === false, $admin->getError());

        // 返回
        $token = Crypt::encryptString(json_encode($user, JSON_UNESCAPED_UNICODE));
        // 权限整理
        $user['roles'] = $admin->makeRoles($user['role'], true); // , $user['id'] == 1 ? true : false
        $user['uid']   = $user['id'];
        // 整理返回
        $result = ['token' => $token, 'user' => $user,];

        return $this->success($result);
    }

    // 生成token
    public function token()
    {
        $admin = new Admin();
        $uid   = $this->getData('uid', 0, 'intval');
        $field = ['id', 'username', 'status', 'nickname', 'mobile',];
        $info  = $admin->where(['id' => $uid])->select($field)->first(); // ->recycle()
        $this->throwIf(empty($info), '用户不存在！！');
        $this->throwIf($info->status === 0, '用户被禁用！！');

        $info   = $info->toArray();
        $token  = Crypt::encryptString(json_encode($info, JSON_UNESCAPED_UNICODE));
        $result = ['token' => $token, 'user' => $info,];

        return $this->success($result);
    }

    // 退出
    public function logout()
    {
        return $this->success();
    }


}


